from abc import ABC, abstractmethod
import os


class BaseView(ABC):
    def __init__(self, delegetor, **kwargs):
        self.path = None
        self.opened = False
        self.config = {}
        self.delegetor = delegetor

    @abstractmethod
    def open(self, path):
        """打开文件。"""
        self.path = path

    @abstractmethod
    def close(self):
        """关闭文件 钩子。如何有编辑，返回True。无操作返回False。用于同步笔记。"""
        return self.opened

    @abstractmethod
    def file_extension(self):
        """返回支持的文件扩展。返回None表示这是一个插件。"""
        pass

    def set_config(self, config: dict):
        self.config = config

    def get_config(self):
        return self.config

    def get_basic_config(self):
        return dict()

    def open_by_others(self, event):
        if self.path is not None:
            if "exec" in self.config:
                os.popen(f'{self.config["exec"]} "{self.path}"')
            else:
                os.popen(f'"{self.path}"')

    def pprint(self, *args, **kwargs):
        print(self.file_extension(), " >>>", *args, **kwargs)

    def is_plugin(self):
        return False
